<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.examName"
                        placeholder="考试名称"
                    ></Input>
                </FormItem>
                <FormItem>
                    <Select
                        v-model="params.examStatus"
                        placeholder="状态"
                        style="width:100px"
                        clearable
                    >
                        <Option
                            v-for="(item, k) in examStatusList"
                            :key="k"
                            :value="k"
                        >{{ item }}</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAdd">新建考试</Button>
            </div>
        </div>
        <div>
            <ExamCell
                v-for="item in list"
                :key="item.id"
                showBtns
                :one="item"
                @click.native="handleDetail(item.examId)"
                @on-push="handleRelease"
                @on-edit="handleEdit"
                @on-delete="handleDelete"
            />
        </div>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { examStatus } from '@/libs/enums'
import ExamCell from './components/exam-cell'
import { getExam, releaseExam, deleteExam } from '@/api/paper'
export default {
    components: {
        ExamCell
    },
    inject: ['nextPageReload'],
    data () {
        return {
            examStatusList: examStatus,
            params: {
                total: 0,
                pageNum: 1,
                pageSize: 10,
                examName: '',
                examStatus: ''
            },
            list: []
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        queryList () {
            let params = {
                examName: this.params.examName,
                pageNum: this.params.pageNum,
                pageSize: this.params.pageSize,
                schoolId: '',
                status: this.params.examStatus,
                userId: ''
            }
            getExam(params).then(res => {
                this.list = res.data.data.list
                this.params.total = res.data.data.total
                this.params.pageNum = res.data.data.pageNum
            })
        },
        clearSearch () {},
        handleAdd () {
            this.$router.push({
                name: 'examAdd'
            })
        },
        handle (id) {
            this.$router.push({
                name: 'teaManageEdit',
                params: {
                    id
                }
            })
        },
        handleRelease (id) {
            let params = {
                examId: id
            }
            releaseExam(params).then(res => {
                if (res.data.code != '0000') {
                    this.$Message.error(res.data.data)
                } else {
                    this.$Message.success('发布成功')
                    this.queryList()
                }
            })
        },
        handleEdit (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'examEdit',
                query: {
                    id
                }
            })
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    let params = {
                        examId: id
                    }
                    deleteExam(params).then(res => {
                        if (res.data.code != '0000') {
                            this.$Message.error(res.data.data)
                        } else {
                            this.$Message.success('删除成功')
                            this.queryList()
                        }
                    })
                }
            })
        },
        handleDetail (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'examDetail',
                query: { id }
            })
        }
    }
}
</script>
